package com.doit.day04

import com.doit.day01.Person

object _02_排序算子 {
  def main(args: Array[String]): Unit = {

    /**
     * sorted
     * sortBy
     * sortWith
     */
    val list: List[Int] = List(1, 34, 44, 22, 33, 53, 87, 65, 76)
    val list1: List[String] = List("1", "2", "11", "22", "3", "33", "4", "34", "45")

    val list2: List[Person] = List(
      new Person(1,"zss",18,"male"),
      new Person(2,"lss",20,"female"),
      new Person(2,"ww",18,"male"),
      new Person(2,"abc",18,"male"),
      new Person(2,"bcd",18,"male")

    )

    val sorted: List[Int] = list.sorted
    val sorted1: List[String] = list1.sorted

    /*list2.sorted(new Ordering[Person] {
      override def compare(x: Person, y: Person) = x.getId.compare(y.getId)
    })*/
    val persons: List[Person] = list2.sortBy((p: Person) => p.getName).reverse
    val persons1: List[Person] = list2.sortBy((p: Person) => (-p.getId,p.getName))/*.reverse*/
    //返回的时候 元祖
    println(persons1)
    println(list)
    println(sorted)
    println(sorted1)

    //id的升序然后name的降序
    val persons2: List[Person] = persons1.sortWith((p, p1) => {
      if (p.getId == p1.getId){
        p.getName > p1.getName
      }else if(p.getId > p1.getId){
        false
      }else{
        true
      }
    })
    persons2.foreach(println)


    val tuples: List[(String, Int)] = List(("zss", 18), ("lss", 20), ("ww", 30), ("zl", 18))
    //sorted   sortBy   sortWith

  }
}
